package at.ttasat;

import java.io.File;
import java.util.Arrays;

public class CreateBase {
	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			System.out.println("CreateBase [--seed=<seed>] [--file=<filename>] [--noOverwrite] <runs> <maxVariables> <maxClauses> <weightingFactor> <tasat-Type t/p/l> <parameters for tastat>");
			return;
		}

		String fileName = "results/<c>_<d>.perf";
		boolean noOverwrite = false;
		long startSeed = System.currentTimeMillis();
		int index = 0;
		boolean wasUsed;
		do {
			wasUsed = true;
			if (args[index].startsWith("--seed=")) {
				startSeed = Long.parseLong(args[index].split("=")[1]);
			} else if (args[index].startsWith("--file=")) {
				fileName = args[index].split("=")[1];
			} else if (args[index].equals("--noOverwrite")) {
				noOverwrite = true;
			} else {
				wasUsed = false;
			}
			if (wasUsed) {
				index++;
			}
			if (args.length < index + 4) {
				throw new IllegalArgumentException();
			}
		} while (wasUsed);

		int runs = Integer.parseInt(args[index]);
		int maximalVariables = Integer.parseInt(args[index + 1]);
		int maximalClauses = Integer.parseInt(args[index + 2]);
		double weightingFactor = Double.parseDouble(args[index + 3]);
		TasatType type = Util.getTasatType(args[index + 4]);
		String[] parameters = Arrays.copyOfRange(args, index + 5, args.length);

		if (noOverwrite) {
			SolverTestResult expectedResult = new SolverTestResult(type, parameters, runs, startSeed, null, maximalVariables, maximalClauses, weightingFactor);
			File expectedFile = new File(Util.getFileName(expectedResult, fileName));
			if (expectedFile.exists()) {
				System.out.println("File '" + expectedFile.getAbsolutePath() + "' already exists");
				return;
			}
		}

		SolverSpeedtest test = new SolverSpeedtest(type, parameters, runs, startSeed, maximalVariables, maximalClauses, weightingFactor);
		SolverTestResult result = test.test();
		Util.save(result, fileName);
		result.printResults();
	}
}
